//
//  AXFHomeViewModel.m
//  AIXF
//
//  Created by Macx on 2017/7/10.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFHomeViewModel.h"
#import "AXFHomeSever.h"
#import <YYModel.h>
#import "AXFSuperMarketSever.h"

@interface AXFHomeViewModel ()

@end
@implementation AXFHomeViewModel

IMPLEMENTATION_SINGLETON(AXFHomeViewModel);


+ (instancetype)shared
{
    static AXFHomeViewModel *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[AXFHomeViewModel alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    if (self  = [super init])
    {
        
        [[AXFHomeSever new] requestHomeFirstSellWith:^(id response) {

            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.firstSellModelArray = [NSArray yy_modelArrayWithClass:[AXFHomeFirstSellModel class] json:response];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"postArray" object:nil];
                
            });
            
        } andFailBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
        }];
        
        
        [[AXFSuperMarketSever new] requestWithSuperMarketWith:^(id response) {
            
            //NSLog(@"%@",response);
            
            NSMutableArray *array = [NSMutableArray array];
            
            NSDictionary *dict = response[@"products"];
            
            self.superMarketLeftModelArray = [NSArray yy_modelArrayWithClass:[AXFSuperMarketModel class] json:response[@"categories"]];
            
            for (AXFSuperMarketModel *model  in  self.superMarketLeftModelArray ) {
                NSArray *modelArray = [NSArray yy_modelArrayWithClass:[AXFHomeFirstSellModel class] json:dict[model.id]];
                [array addObject:modelArray];
            }
            
            self.superMarketRightModelArray = array.copy;
            
            //NSLog(@"%@ %@",self.superMarketRightModelArray,self.superMarketLeftModelArray);
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"postSuperMarketArray" object:nil];
            
        } andFailBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
           
        }];
        
        [[AXFHomeSever new] requestHomeSearchReult:^(id response) {
            
            self.searchModelArray = [NSArray yy_modelArrayWithClass:[AXFHomeFirstSellModel class] json:response];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"postSearchArray" object:nil];
            
    
        } andFailBlock:^(NSError *error) {
            
        }];
    }
    return self;
}


@end
